package com.vp.org.web.servlet;

import com.vp.org.dao.*;
import com.vp.org.dao.impl.*;
import com.vp.org.pojo.AdminInfo;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends javax.servlet.http.HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {

        AdminInfo adminInfo = new AdminInfo();


        AdminInfoDao adminInfoDao = new AdminInfoDaoImpl();
        BookInfoDao bookInfoDao = new BookInfoDaoImpl();
        RemarkDao remarkDao = new RemarkDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        RecordDao recordDao = new RecordDaoImpl();
        IntegralExchangeDao integralExchangeDao = new IntegralExchangeDaoImpl();
        UserInfoDao userInfoDao = new UserInfoDaoImpl();


        // 获取请求
        String admin = request.getParameter("admin");
        String password = request.getParameter("password");

        // 封装对象
        adminInfo.setAdmin(admin);
        adminInfo.setPassword(password);

        // 调用方法
        AdminInfo adminInfos = adminInfoDao.adminLogin(adminInfo);

        if (adminInfos != null){ // 登录成功

            if ("gsh".equals(adminInfo.getAdmin())){

                request.getSession().setAttribute("adminInfos",adminInfos);
                adminInfo.setAdmin(admin);

                // 供应商分页查询,默认第一页
                request.getSession().setAttribute("admins", adminInfoDao.queryAdmin(1));
                // 供应商分页总页数
                request.getSession().setAttribute("adminPage", adminInfoDao.adminPage());
                // 供应商存储是第几页
                request.getSession().setAttribute("countPage",1);


                // 地址重定向
                response.sendRedirect("admin/adminIndex.jsp");

            } else {
                request.getSession().setAttribute("adminInfos",adminInfos);
                adminInfo.setAdmin(admin);

                // 书籍分页查询,默认第一页
                request.getSession().setAttribute("bookInfos", bookInfoDao.adminBookPagingQuery(1,7));
                // 书籍分页总页数
                request.getSession().setAttribute("bookPage", bookInfoDao.adminQueryBookPage());
                // 书籍存储是第几页
                request.getSession().setAttribute("currentPage",1);



                // 评论分页查询,默认第一页
                request.getSession().setAttribute("remarks", remarkDao.queryPageRemark(1));
                // 评论分页总页数
                request.getSession().setAttribute("remarkPage", remarkDao.remarkPages());
                // 评论存储是第几页
                request.getSession().setAttribute("currentPage2",1);


                // 订单分页查询,默认第一页
                request.getSession().setAttribute("orders",ordersDao.ordersPageQuery(adminInfo,1));
                // 订单分页总页数
                request.getSession().setAttribute("ordersPage",ordersDao.ordersPage());
                // 订单存储是第几页
                request.getSession().setAttribute("currentPage3",1);


                // 热销图书分页查询,默认第一页
                request.getSession().setAttribute("promotions",bookInfoDao.promotionBooks(1));
                // 热销图书分页总页数
                request.getSession().setAttribute("promotionPage",bookInfoDao.promotionPage());
                // 热销图书存储是第几页
                request.getSession().setAttribute("currentPage4",1);


                // 销售总额
                request.getSession().setAttribute("sale",ordersDao.saleOrders());


                // 兑换记录分页查询,默认第一页
                request.getSession().setAttribute("records",recordDao.queryPage(1));
                // 兑换记录分页总页数
                request.getSession().setAttribute("recordPage",recordDao.recordPage());
                // 兑换记录存储是第几页
                request.getSession().setAttribute("currentPage5",1);


                // 积分兑换分页查询,默认第一页
                request.getSession().setAttribute("exchanges",integralExchangeDao.queryPage(1));
                // 积分兑换分页总页数
                request.getSession().setAttribute("exchangePage",integralExchangeDao.exchangePage());
                // 积分兑换存储是第几页
                request.getSession().setAttribute("currentPage6",1);


                // 会员管理分页查询,默认第一页
                request.getSession().setAttribute("users",userInfoDao.selectUserInfo(1));
                // 会员管理分页总页数
                request.getSession().setAttribute("userPage",userInfoDao.page());
                // 会员管理存储是第几页
                request.getSession().setAttribute("currentPage7",1);


                // 地址重定向
                response.sendRedirect("admin/index.jsp");
            }


        } else { // 登录失败

            request.setAttribute("msg","用户名或密码错误！，请重新登录！");
            request.getRequestDispatcher("admin/login.jsp").forward(request,response);

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doPost(request, response);
    }
}
